﻿using Models.DataModel;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models.DataStore
{
    public class ContributionRateExtra : ContributionRate
    {
        public String FundCode { get; set; }
        public String PortName { get; set; }
    }
    public partial class Ctrl : Core
    {
        /// <summary>
        /// danh sach ContributionRate
        /// get danh sach ContributionRateExtra gom ten port va code fund
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public List<ContributionRateExtra> GetListContributionRate(String temp = "")
        {
            if (temp != null && temp.Length > 0)
                return (from cbr in db.ContributionRates
                        join fund in db.Funds on cbr.FundId equals fund.FundId
                        join port in db.Ports on cbr.PortId equals port.PortId
                        where port.Name.Contains(temp) || fund.Code.Contains(temp)
                        select new ContributionRateExtra
                        {
                            ContributionRateId = cbr.ContributionRateId,
                            Year = cbr.Year,
                            FundId = cbr.FundId,
                            LaborType = cbr.LaborType,
                            PortNumber = cbr.PortNumber,
                            PortId = cbr.PortId,
                            Rate = cbr.Rate,
                            Percentage = cbr.Percentage,
                            CreatedOn = cbr.CreatedOn,
                            ModifiedOn = cbr.ModifiedOn,
                            CreatedByName = cbr.CreatedByName,
                            FundCode = fund.Code,
                            PortName = port.Name
                        }).ToList();
            else
                return (from cbr in db.ContributionRates
                        join fund in db.Funds on cbr.FundId equals fund.FundId
                        join port in db.Ports on cbr.PortId equals port.PortId
                        select new ContributionRateExtra
                        {
                            ContributionRateId = cbr.ContributionRateId,
                            Year = cbr.Year,
                            FundId = cbr.FundId,
                            LaborType = cbr.LaborType,
                            PortNumber = cbr.PortNumber,
                            PortId = cbr.PortId,
                            Rate = cbr.Rate,
                            Percentage = cbr.Percentage,
                            CreatedOn = cbr.CreatedOn,
                            ModifiedOn = cbr.ModifiedOn,
                            CreatedByName = cbr.CreatedByName,
                            FundCode = fund.Code,
                            PortName = port.Name
                        }).ToList();

        }
        /// <summary>
        /// Get ContributionRate by Guid Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ContributionRate GetContributionRateById(Guid id)
        {
            return db.ContributionRates.Where(ctr => ctr.ContributionRateId == id).FirstOrDefault();
        }
        /// <summary>
        /// return Code fund 
        /// </summary>
        /// <param name="fundid"></param>
        /// <returns></returns>
        public string GetFundCodeById(Guid fundid)
        {
            return db.Funds.Where(f => f.FundId == fundid).FirstOrDefault().Code;
        }
        /// <summary>
        /// get port number by id
        /// </summary>
        /// <param name="portid"></param>
        /// <returns></returns>
        public string GetPortNumberById(Guid portid)
        {
            return db.Ports.Where(f => f.PortId == portid).FirstOrDefault().PortNumber;
        }
        /// <summary>
        /// Insert New ContributionRate
        /// return string if have error
        /// </summary>
        /// <param name="conrate"></param>
        /// <returns></returns>
        public String InsertContributionRate(ContributionRate contributionRate)
        {
            try
            {
                contributionRate.ContributionRateId = Guid.NewGuid();
                contributionRate.CreatedOn = DateTime.Now;
                contributionRate.ModifiedOn = DateTime.Now;
                contributionRate.CreatedByName = "ADMIN";
                db.Entry(contributionRate).State = EntityState.Added;
                db.SaveChangesAsync();
                return null;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
        /// <summary>
        /// update ContributionRate 
        /// </summary>
        /// <param name="contributionrate"></param>
        /// <returns></returns>
        public async Task<String> UpdateContributionRate(ContributionRate contributionrate)
        {
            try
            {
                contributionrate.ModifiedOn = DateTime.Now;
                db.Entry(contributionrate).State = EntityState.Modified;
                db.SaveChangesAsync();
                return null;
            }
            catch (Exception e) { 
                return e.ToString(); 
            }

        }
        public bool CheckPortNumber(string portnumber)
        {
            var temp = db.Ports.Where(p => p.PortNumber == portnumber.Trim()).FirstOrDefault();
            if (temp != null)
                return false;
            return true;
        }
        public int DeleteContributionRate(Guid id)
        {
            var cd = db.ContributionRates.Where(co => co.ContributionRateId == id).FirstOrDefault();
            db.Entry(cd).State = EntityState.Deleted;
            return db.SaveChanges();
        }
        /// <summary>
        /// xoa nhieu dong trong list
        /// </summary>
        /// <param name="contributionRateItemDelete"></param>
        /// <returns></returns>
        /// 
        public int DeleteSelectedContributionRate(IEnumerable<Guid> contributionRateItemDelete)
        {
            try
            {
                List<ContributionRate> list = db.ContributionRates.Where(cp => contributionRateItemDelete.Contains(cp.ContributionRateId)).ToList<ContributionRate>();
                db.ContributionRates.RemoveRange(list);
                return db.SaveChanges();
            }
            catch { return 0; }


        }
    }
}
